<?php
class ome_ctl_admin_delivery extends desktop_controller{
    var $name = "发货单";
    var $workground = "invoice_center";

    function index(){
        $filter = array(
            'type' => 'normal',
            'pause' => 'false',
            'parent_id' => 0,
            'disabled' => 'false',
            'status' => array('ready','progress','succ')
        );
        $this->finder('ome_mdl_delivery',array(
            'title' => '发货单管理',
            'base_filter' => $filter,
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>true,
       ));
    }
    
    function reback(){
    	$this->page('admin/delivery/reback_delivery.html');
    }
    
    function back(){
    	$this->begin();
    	$autohide = array('autohide'=>3000);
    	$Objdly  = &$this->app->model('delivery');
    	$OiObj  = &$this->app->model('delivery_items');
        if (empty($_POST['select_bn']) && empty($_POST['bn_select'])){
            $this->end(false, '请输入正确的单号', '', $autohide);
        }
        if($_POST['select_bn'] == 'logi_no'){
        	$detail = $Objdly->dump(array('logi_no'=>$_POST['bn_select']));
        }else{
        	$detail = $Objdly->dump(array('delivery_bn'=>$_POST['bn_select']));
        }
        $items = $OiObj->getList('*',array('delivery_id'=>$detail['delivery_id']));
        if(empty($detail)){
        	$this->end(false, '没有该单号的发货单', '', $autohide);
        }
        if($detail['status'] == 'back'){
        	$this->end(false, '该发货单已经被打回，无法继续操作', '', $autohide);
        }
        if($detail['pause'] == 'true'){
            $this->end(false, '该发货单已暂停，无法继续操作', '', $autohide);
        }
        if($detail['process'] == 'true'){
            $this->end(false, '该发货单已经发货，无法继续操作', '', $autohide);
        }
        $pObj = &$this->app->model('products');
        foreach($items as $k=>$value){
        	$barcode = $pObj->dump(array('product_id'=>$value['product_id']),'barcode');
        	$items[$k]['barcode'] = $barcode['barcode'];
        }
        $this->pagedata['items'] = $items;
        $this->pagedata['detail'] = $detail;
        $this->page('admin/delivery/reback_delivery_mark.html');
    }
    
    /**
     * 打回操作
     *
     */
    function doReback(){
        $autohide = array('autohide'=>3000);
        $this->begin('index.php?app=ome&ctl=admin_delivery&showmemo&p[0]='.$_POST['id']);
        if (empty($_POST['id']) && !empty($_POST['flag'])){
            $this->end(false, '请选择至少一张发货单', '', $autohide);
        }
        if (empty($_POST['memo'])){
            $this->end(false, '备注请不要留空', '', $autohide);
        }
        $Objdly  = &$this->app->model('delivery');
        $opObj = &$this->app->model('operation_log');
        $doObj = &$this->app->model('delivery_order');
        if ($_POST['flag'] == 'OK'){
            $allids = explode(',', $_POST['returnids']);
            if (sizeof($allids) - sizeof($_POST['id']) < 2){
                $result = $Objdly->splitDelivery($_POST['delivery_id']);
            }else {
                $result = $Objdly->splitDelivery($_POST['delivery_id'], $_POST['id']);
            }
            if ($result){
                $Objdly->rebackDelivery($_POST['id'], $_POST['memo']);
                foreach ($_POST['id'] as $i){
                    $opObj->write_log('delivery_back@ome', $i ,'发货单打回');
                    $order_ids= $doObj->dump(array('delivery_id'=>$i),'order_id');
                    $delivery_bn = $Objdly->dump(array('delivery_id'=>$i),'delivery_bn');
                    $opObj->write_log('order_back@ome', $order_ids['order_id'], '发货单'.$delivery_bn['delivery_bn'].'打回+'.'备注：'.$_POST['memo']); 
                    $Objdly->updateOrderPrintFinish($i, 1);
                }
                $this->end(true, '操作成功', 'index.php?app=ome&ctl=admin_delivery&act=reback', $autohide);
            }else {
                $this->end(false, '操作失败', '', $autohide);
            }
        }else {
            $Objdly->rebackDelivery($_POST['delivery_id'], $_POST['memo']);    
            $opObj->write_log('delivery_back@ome', $_POST['delivery_id'], '发货单打回');
            $order_ids= $doObj->dump(array('delivery_id'=>$_POST['delivery_id']),'order_id');
            $delivery_bn = $Objdly->dump(array('delivery_id'=>$_POST['delivery_id']),'delivery_bn');
            $opObj->write_log('order_back@ome', $order_ids['order_id'], '发货单'.$delivery_bn['delivery_bn'].'打回+'.'备注:'.$_POST['memo']); 
            $Objdly->updateOrderPrintFinish($_POST['delivery_id'], 1);
            
            $this->end(true, '操作成功', 'index.php?app=ome&ctl=admin_delivery&act=reback', $autohide);
        }
    }
    
    /**
     * 填写打回备注
     *
     * @param bigint $dly_id
     */
    function showmemo($dly_id){
        $deliveryObj  = &$this->app->model('delivery');
        $dly          = $deliveryObj->dump($dly_id);
        
        if ($dly['is_bind'] == 'true'){
          $ids = $deliveryObj->getItemsByParentId($dly_id, 'array');
          $returnids = implode(',', $ids);
          $idd = array();
          if ($ids)
          foreach ($ids as $v){
              $delivery = $deliveryObj->dump($v, 'delivery_bn');
              $idd[$v] = $delivery['delivery_bn'];
          }
          $this->pagedata['returnids'] = $returnids;
          $this->pagedata['ids'] = $ids;
          $this->pagedata['idd'] = $idd;
        }
        
        $this->pagedata['delivery_id'] = $dly_id;
        $this->display("admin/delivery/delivery_showmemo.html");
    }
    
}
?>
